package com.demo.serviceimpl;

import com.demo.service.ExecutePythonService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.Charset;

@Service
public class ExecutePythonServiceImpl implements ExecutePythonService {
    /**
     * 执行python脚本
     *
     * @param sno      账号
     * @param password 密码
     * @return html文本
     */
    @Override
    public String getHtmlByExecutePython(String sno, String password) {
        Process proc;
        StringBuilder stringBuilder=new StringBuilder();
        try {
            proc = Runtime.getRuntime().exec("python getLessons.py");
            OutputStream outputStream = proc.getOutputStream();
            sno=sno+"\n";
            password=password+"\n";
            outputStream.write(sno.getBytes());
            outputStream.flush();
            outputStream.write(password.getBytes());
            outputStream.flush();
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader errorIn=new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line;
            while ((line = in.readLine()) != null) {
                stringBuilder.append(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
